package com.contory.ssn_lgnd.app.title;

import android.opengl.GLES20;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.ESShaderHandleNames;
import com.contory.ssn_lgnd.app.GameScreenLoader;
import com.contory.ssn_lgnd.framework.Input.TouchEvent;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.impl.GLGame;

public class TitleState_End extends TitleScene{
	final Title title;
	
	public TitleState_End(Title title){
		this.title = title;
	}
	
	@Override
	public void draw(SpriteBatcher batcher) {
		title.fx_glow_defalutRender.bind();
		title.fx_glow_defalutRender.beginBatch(Assets.tex1);
			title.sky.draw(batcher);
			title.sun.draw(batcher);
			title.sea.draw(batcher);
			title.player.draw(batcher);
			title.cloud.draw(batcher);
			title.fx_glow_defalutRender.endBatch();
		title.fx_glow_defalutRender.unBind();
		
		title.fx_glow_blurRender.setUniforms(0, 10, 1.0f, 0.2f);
		title.fx_glow_blurRender.bind();
		title.fx_glow_blurRender.beginBatch(title.fx_glow_defalutRender.getTexture());
			title.batcher.drawSprite(10, 5, 20, 10, 1, 1, 1, 1, title.fx_glow_defalutRender.getTextureRegion());
		title.fx_glow_blurRender.endBatch();
		title.fx_glow_blurRender.unBind();
		
		title.fx_glow_blurRender.setUniforms(1, 10, 1.0f, 0.2f);
		title.fx_glow_blurRender.bind();
		title.fx_glow_blurRender.beginBatch(title.fx_glow_blurRender.getTexture());
       		batcher.drawSprite(10, 5, 20, 10, 1, 1, 1, 1, title.fx_glow_blurRender.getTextureRegion());
   		title.fx_glow_blurRender.endBatch();
   		title.fx_glow_blurRender.unBind();
		
   		title.fx_glow_additiveRender.setUniforms(1);
   		title.fx_glow_additiveRender.bind();
   		title.fx_glow_additiveRender.beginBatch(title.fx_glow_defalutRender.getTexture(), title.fx_glow_blurRender.getTexture());
			batcher.drawSprite(10, 5, 20, 10, 1, 1, 1, 1, title.fx_glow_blurRender.getTextureRegion());
		title.fx_glow_additiveRender.endBatch();
		title.fx_glow_additiveRender.unBind();
		
		GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
		title.cam.setViewPort(GLGame.frameWidth, GLGame.frameHeight);
		title.shader_default.bind();
	    title.cam.setMatrices(title.shader_default, ESShaderHandleNames.Uni_MvpMatrix);
		batcher.beginBatch(title.shader_default, title.fb3.getTexture(), ESShaderHandleNames.Uni_Sample0, 0);
        batcher.drawSprite(10, 5, 20, 10, 1.0f, 1.0f, 1.0f, 1.0f, title.fb3.getTextureRegion());
        batcher.endBatch(title.shader_default, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
		
        title.shader_curtain.bind();
        GLES20.glUniform1f(title.shader_curtain.getUniform(ESShaderHandleNames.Uni_InnerVignetting), 1.0f - title.curtain.getVignettingValue());
        GLES20.glUniform1f(title.shader_curtain.getUniform(ESShaderHandleNames.Uni_OuterVignetting), 1.4f - title.curtain.getVignettingValue());
        title.cam.setMatrices(title.shader_curtain, ESShaderHandleNames.Uni_MvpMatrix);
		batcher.beginBatch(title.shader_curtain, Assets.tex2, ESShaderHandleNames.Uni_Sample0, 0);
        title.curtain.draw(batcher);
		batcher.endBatch(title.shader_curtain, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
	}

	@Override
	public void update(float deltaTime) {
		title.curtain.update(deltaTime);
		title.cloud.update(deltaTime);
		title.wave.update(deltaTime);
		title.player.update(deltaTime);
		if(!title.curtain.getEnableBool()){
			GameScreenLoader.gameLoad(title.getGLGame(), GameScreenLoader.SCRENN_MAINGAME);
		}
	}

	@Override
	public void setTouchEvent(TouchEvent touchEvent, float touchX, float touchY) {
		//stub
	}

}
